package com.kangye.exam.controller;

import com.github.pagehelper.PageInfo;
import com.kangye.exam.bean.Gonggao;
import com.kangye.exam.service.GonggaoService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @author 海风
 * 对公告表的增删改查操作
 * @data 2021-3-15 18:45:09
 * @since 1.1.5
 * */
@RestController
@RequestMapping("/gonggao")
@CrossOrigin
public class GonggaoController {

    @Resource
    private GonggaoService gonggaoService;

    /*
     * @Description 通过时间查找公告
     * */
    @RequestMapping("/findByTime")
    public Object findByTime(String currentTime)  {
        Map<String,Object> map=new HashMap<>();
        List<Gonggao> gonggao2=gonggaoService.selectByTime(currentTime);
        map.put("data",gonggao2);
        return map;
    }
    /*
     * 按条件 模糊查询 分页
     * @content  模糊查询的条件
     * @pageNum 当前页
     * @pageSize 每页条数
     * */
    @RequestMapping("/limit")
    public Object find_limit(String content, Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        PageInfo<Gonggao> pageInfo = gonggaoService.find_limit(map, page, limit);
        map.clear();
        map.put("code", 0);
        map.put("data", pageInfo.getList());
        map.put("count", pageInfo.getTotal());
        map.put("limit", pageInfo.getPageSize());
        return map;
    }

    /*
     * @Description 通过id查找公告
     * @id 公告id
     * */
//    @RequestMapping("/find/{id}")
//    public Object find_id(@PathVariable("id") Integer id) {
//        Map<String, Object> map = new HashMap<>();
//        Gonggao gonggao = gonggaoService.selectId(id);
//        map.put("code",0);
//        map.put("data", gonggao);
//        return map;
//    }

    /*
     * 插入一条公告
     * @gonggao 公告class
     * */
    @RequestMapping("/insert")
    public Object insert(Gonggao gonggao) {
        Date date = new Date();
        gonggao.setCreateTime(date);
        return gonggaoService.insert(gonggao) > 0?"{\"code\":0}":"{\"code\":-1}";
    }

    /*
     * 修改一条公告
     * @gonggao 公告class,判空
     * */
    @RequestMapping("/update")
    public Object update(Gonggao gonggao) {
        return gonggaoService.update(gonggao) > 0 ? "{\"code\":0}" : "{\"code\":-1}";
    }

    /*
     * 删除一条公告
     * @id 需要删除的公告id
     * */
    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id) {
        return gonggaoService.delete(id) > 0 ? "{\"code\":0}" : "{\"code\":-1}";
    }
}
